<template>
  <div>
    <el-card class="box-card">
      <!-- 面包屑导航 -->
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>管理设置</el-breadcrumb-item>
          <el-breadcrumb-item active>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-button
        type="success"
        style="margin-top: 30px"
        @click="addDialogVisible = true"
        >添加角色</el-button
      >
      <el-table
        :data="datalist"
        height="500"
        row-key="rid"
        border
        style="width: 100%; margin-top: 30px; height: 100%"
      >
        <el-table-column prop="rid" label="角色编号" width="180">
        </el-table-column>
        <el-table-column prop="rolename" label="角色名称" width="180">
        </el-table-column>
        <el-table-column label="权限列表">
          <template slot-scope="scope">
            <el-tag
              v-for="item in scope.row.role"
              :key="item.pid"
              style="margin-right: 8px"
              >{{ item.pname }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="editDialog(scope.row)"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              @click="deleteDialog(scope.row)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加按钮 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="pass">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限选择" prop="pass">
          <!-- <el-input v-model="form.pname" autocomplete="off"></el-input> -->
          <el-select v-model="form.powerid" multiple placeholder="请选择">
            <el-option
              v-for="item in dataPowerlist"
              :key="item.pid"
              :label="item.pname"
              :value="item.pid"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="addPoWer">确 定</el-button>
      </span>
    </el-dialog>
        <!-- 修改按钮 -->
    <el-dialog
      title="修改角色"
      :visible.sync="editDialogVisible"
      width="30%"
      center
      :before-close="clearDialog"
    >
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="pass">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限选择" prop="pass">
          <!-- <el-input v-model="form.pname" autocomplete="off"></el-input> -->
          <el-select v-model="form.powerid" multiple placeholder="请选择">
            <el-option
              v-for="item in dataPowerlist"
              :key="item.pid"
              :label="item.pname"
              :value="item.pid"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearDialog">取 消</el-button>
        <el-button type="primary" @click="editPoWer">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addrole,editrole,delrole } from "../../http/api";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      form: {
        rolename: "",
        powerid: [],
      },
    };
  },
  created() {
    this.getrolelist();
    this.getpowerlist()
  },
  methods: {
    ...mapActions({
      "getrolelist": "role/getrolelistActions",
      "getpowerlist":"power/getpowerlistActions"
    }),
    // 取消操作
    clearDialog() {
      this.editDialogVisible = false;
      this.addDialogVisible = false;
      this.form = {
        rid:null,
        rolename: "",
        powerid: [],
      };
    },
    //添加角色
    addPoWer() {
       let that = this;
      addrole(this.form).then(res=>{
        if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getrolelist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      })
      this.clearDialog();
    },

    //修改角色
    editPoWer(){
      let that = this;
      editrole(this.form).then(res=>{
        if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getrolelist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
      })
      this.clearDialog();
    },
    // 打开修改对话框
    editDialog(data){
      this.editDialogVisible = true;
      console.log(data);
      this.form.rolename=data.rolename
      this.form.rid=data.rid
      data.role.forEach(ele => {
        this.form.powerid.push(ele.pid)
      });
    },
        deleteDialog(data) {
        let that = this;
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
            delrole(data).then(res=>{
                if (res.status == 201) {
        //   that.datalist = res.data;
          that.$message.success(res.message);
          that.getrolelist();
        } else {
          console.log(res);
          that.$message.error(res.message);
        }
            })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  computed: {
    ...mapGetters({
      "datalist": "role/datalist",
      "dataPowerlist":"power/datalist"
    }),
  },
};
</script>

<style>
</style>